<?php
include('includes/connexion.php');
$elevemanager = new EleveManager($connexion);
session_start();
$resultatmanager = new ResultatManager($connexion);
$accesmanager = new AccesManager($connexion);
$exercicemanager = new ExerciceManager($connexion);
$questionmanager = new QuestionManager($connexion);
$reponsemanager = new ReponseManager($connexion);
$test_exercicemanager = new Test_ExerciceManager($connexion);
?>

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Dashboard - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="css/font-awesome.min.css">
  <link rel="stylesheet" href="js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="js/plugins/morris/morris.css">
  <link rel="stylesheet" href="js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">

  <!-- App CSS -->
  <link rel="stylesheet" href="css/target-admin.css">
  <link rel="stylesheet" href="css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->

</head>

<body>

<?php

include('includes/navbar.php');
include('includes/mainbar.php');

//Choix exemple
$acces = $accesmanager->checkByClasse($_SESSION['Eleve']->idRefClasse());

$arrIdEx = $resultatmanager->ListExercice($_SESSION['Eleve']->id(), $acces->idRefTest()); //Exercices déjà effectués
$arrEx = $test_exercicemanager->getByTest($acces->idRefTest()); //Exercices du test

if(isset($_GET['ip']))
{
	$newArrEx = array();
	foreach($arrEx as $ex)
	{
		if($exercicemanager->get($ex->idRefExercice())->type() != 'IP')
		{
			$newArrEx[] = $ex;
		}
	}
	$arrEx = $newArrEx;
}

if(isset($_GET['q']))
{
	foreach($arrEx as $ex)
	{
		if($exercicemanager->get($ex->idRefExercice())->type() == 'Q')
		{
			$arrIdEx[] = $ex->idRefExercice();
		}
	}
}

//Initialise le prochain exercice à faire
if(empty($arrIdEx)){
	$IdExemple = $arrEx['0']->idRefExercice();
} else {
	foreach($arrEx as $value2){
		$existe = false;
		foreach($arrIdEx as $value1)
		{
			if($value2->idRefExercice() == $value1)
			{
				$existe = true;
			}
		}
		if(!$existe)
		{
			$IdExemple = $value2->idRefExercice();
			break;
		}
	}
}

//Si tous les exercices ont été traités
if($IdExemple == NULL)
{
	header('Location: test.php?end');
}

//Si il n'y a pas d'exemple
if(!isset($_GET['ex']) && $exercicemanager->getExemple($IdExemple) == NULL)
{
	if(isset($_GET['ip']) && isset($_GET['q']))
	{
		header('Location: ?ex&ip&q');
	}
	elseif(isset($_GET['ip']))
	{
		header('Location: ?ex&ip');
	}
	elseif(isset($_GET['q']))
	{
		header('Location: ?ex&q');
	}else{
		header('Location: ?ex');
	}
}

if(!isset($_GET['ex'])){
	if($resultatmanager->getByExerciceEleveTest($exercicemanager->getExemple($IdExemple)->id(), $_SESSION['Eleve']->id(),$acces->idRefTest()) != false){
		if(isset($_GET['ip']) && isset($_GET['q']))
		{
			header('Location: ?ex&ip&q');
		}
		elseif(isset($_GET['ip']))
		{
			header('Location: ?ex&ip');
		}
		elseif(isset($_GET['q']))
		{
			header('Location: ?ex&q');
		}else{
			header('Location: ?ex');
		}
	}
}

if(!isset($_GET['ex'])){
	$exemple = $exercicemanager->getExemple($IdExemple);
} else {
	$exemple = $exercicemanager->get($IdExemple);
}

if($exemple == false){
	if(isset($_GET['ip']) && isset($_GET['q']))
	{
		header('Location: ?ex&ip&q');
	}
	elseif(isset($_GET['ip']))
	{
		header('Location: ?ex&ip');
	}
	elseif(isset($_GET['q']))
	{
		header('Location: ?ex&q');
	}else{
		header('Location: ?ex');
	}
}
?>

<div class="container">

  <div class="content">

    <div class="content-container">
	<br><br>
      <div class="content-header">
        <h2 class="content-header-title">Test en cours...</h2>
      </div> <!-- /.content-header --> 

      <div class="row">

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Test en cours...
              </h3>
              
            </div> <!-- /.portlet-header -->

            <?php 
			//Txt a trou
			
			if($exemple->type() == "D"){
				include('includes/reponseDominos.php');
			} elseif($exemple->type() == "R" || $exemple->type() == "A" || $exemple->type() == "SN" || $exemple->type() == "BB" || $exemple->type() == "G"){
				include('includes/reponseGenerique.php');	
			}elseif($exemple->type() == "IP") {
				if(isset($_GET['q'])){header('Location: interetspro.php?id='.$exemple->id().'&q');}
				else{header('Location: interetspro.php?id='.$exemple->id());}
			}elseif($exemple->type() == "Q") {
				if(isset($_GET['ip'])){header('Location: questionnaire.php?id='.$exemple->id().'&ip');}
				else{header('Location: questionnaire.php?id='.$exemple->id());}
			}elseif($exemple->type() == "TT") {
				include('includes/reponseTxtATrou.php');
			} else {
				include('includes/reponseGenerique.php');
			}
			?>

          </div> <!-- /.portlet -->

        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="js/libs/jquery-1.10.1.min.js"></script>
  <script src="js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->

  <!-- App JS -->
  <script src="js/target-admin.js"></script>
  
    <script type="text/javascript">
	var countdown = <?php echo $exemple->duree_seconde(); ?>;
	var secon = countdown%60;
    var minu = Math.floor(countdown/60);
    document.getElementById("secondes").value=" "+secon //on affiche les secondes
    document.getElementById("minutes").value=" "+minu //on affiche les minutes
    
	function minuteur(){
	secon--;
    if (secon<0){secon=59;minu--} //si les secondes > 59, on les réinitialise à 0 et on incrémente les minutes de 1
	if(minu < 0){ document.getElementById("partie-save").submit(); }
    document.getElementById("secondes").value=" "+secon //on affiche les secondes
    document.getElementById("minutes").value=" "+minu //on affiche les minutes
    compte=setTimeout('minuteur()',1000) //la fonction est relancée toutes les secondes
	}
	
	minuteur();
</script>
</body>
</html>